package com.zretc.kscxmz.controller;

import com.github.pagehelper.PageInfo;
import com.zretc.kscxmz.entity.Comment;
import com.zretc.kscxmz.entity.Discuss;
import com.zretc.kscxmz.entity.Reply;
import com.zretc.kscxmz.services.DiscussService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zengyanjun
 * @date 2020-07-26
 * 讨论控制层
 */
@Controller
public class DiscussController {
    Logger logger = LoggerFactory.getLogger(DiscussController.class);
    @Autowired
    private DiscussService discussService;

    //添加
    @RequestMapping(value = "/addDiscuss", method = RequestMethod.GET)
    @ResponseBody
    public String addDiscuss(HttpServletRequest request) {
        //获取参数
        String statusId = request.getParameter("typeId");
        String title = request.getParameter("title");
        String content = request.getParameter("discussContent");
        String userId = request.getParameter("userId");

        Discuss discuss = new Discuss();
        discuss.setTypeId(Integer.valueOf(statusId));
        discuss.setDiscussUserId(Integer.valueOf(userId));
        discuss.setTitle(title);
        discuss.setContent(content);
        int add = discussService.insert(discuss);
        return add + "";
    }

    //修改
    @RequestMapping(value = "/updateDiscuss", method = RequestMethod.GET)
    @ResponseBody
    public String updateDiscuss(HttpServletRequest request) {
        //获取参数
        String statusId = request.getParameter("typeId");
        String title = request.getParameter("title");
        String content = request.getParameter("discussContent");
        String discussId = request.getParameter("discussId");

        Discuss discuss = new Discuss();
        discuss.setDiscussId(Integer.valueOf(discussId));
        discuss.setTitle(title);
        discuss.setContent(content);
        int update = discussService.modify(discuss);
        return update + "";
    }

    //查找
    @RequestMapping("/getDiscuss")
    @ResponseBody
    public PageInfo<Discuss> getDiscuss(@RequestParam String pageNum,
                                        @RequestParam String pageSize,
                                        @RequestParam String condition,
                                        @RequestParam String userId) {
        Map<String, String> map = new HashMap<>();
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);
        map.put("condition", condition);
        map.put("userId", userId);
        return discussService.get(map);
    }

    @RequestMapping("/getDiscussById")
    @ResponseBody
    public Discuss getDiscussById(@RequestParam String discussId) {
        return discussService.getDiscussById(Integer.valueOf(discussId));
    }

    //查找指定评论的内容（包括：讨论内容、回复内容、回复下的二级评论）
    @RequestMapping("/getDiscussDetails")
    @ResponseBody
    public Discuss getDiscussDetails(@RequestParam String discussId,@RequestParam String visitNum){
        int discuss_Id = Integer.valueOf(discussId);
        String visit_Num = visitNum;
        Discuss discuss = discussService.findDiscussDetailsById(discuss_Id,visit_Num);
        logger.info(discuss.getTitle());
        logger.info("======回复的信息========");
        for (Reply reply:discuss.getReplies()) {
            logger.info("回复的内容："+reply.getContent());
            logger.info("回复的id："+reply.getReplyId());
            logger.info("回复人："+reply.getReplyUserName());
            logger.info("总评论数："+reply.getComments().size());
            logger.info("=======回复下评论的信息==========");
            for (Comment comment :reply.getComments()) {
                logger.info("评论的内容："+comment.getContent());
                logger.info("评论人："+comment.getUserName());
            }
        }
        return discuss;
    }

    /**
     * 添加回复、评论
     * @param commentId
     * @param replytype
     * @param comment
     * @param userId
     * @return
     */
    @RequestMapping("/sendComment")
    @ResponseBody
    public String sendComment(@RequestParam String commentId,
                              @RequestParam String replytype,
                              @RequestParam String comment,
                              @RequestParam String userId){

        return discussService.addReplyOrComment(commentId, replytype, comment, userId)+"";
    }

    /**
     * 删除指定的回复或者评论
     * @param commentId
     * @param replytype
     * @return
     */
    @RequestMapping("/removeComment")
    @ResponseBody
    public String removeComment(@RequestParam String commentId,
                                @RequestParam String replytype){
        return discussService.removeReplyOrComment(commentId, replytype)+"";
    }
}

